/**
 * 规则：
 * 一、例如：index/index，shop/index以index结尾的，path和name默认去除index
 * 二、例如：shop/list，默认生成name为shop_list（如果结尾为index，例如shop/index则是shop）
 * 三、填写后不会自动生成
 * **/
let routes = [
    {
        path: '/',
        name: 'layout',
        redirect: {name:'index'},
        // component: () => import('@/views/layout.vue'),
        component: 'layout',
        children: [
            {
                // path: '/index',
                // name: 'index',
                meta: {title: '后台首页'},
                component: 'index/index'
            },
            {
                // path: '/shop/goods/list',
                // name: 'shop_goods_list',
                meta: {title: '商品列表'},
                component: 'shop/goods/list'
            },
            {
                meta: {title: '创建商品'},
                component: 'shop/goods/create'
            },
            {
                meta: {title: '相册管理'},
                component: 'image/index'
            },
            {
                meta: {title: '分类管理'},
                component: 'shop/category/list'
            },
            {
                meta: {title: '商品规格管理'},
                component: 'shop/sku/list'
            },
            {
                meta: {title: '商品类型管理'},
                component: 'shop/type/list'
            },
            {
                meta: {title: '商品评论管理'},
                component: 'shop/comment/list'
            },
            {
                meta: {title: '发票管理'},
                component: 'order/invoice/list'
            },
            {
                meta: {title: '售后服务'},
                component: 'order/after-sale/list'
            },
            {
                meta: {title: '订单管理'},
                component: 'order/order/list'
            },
            {
                meta: {title: '会员列表'},
                component: 'user/user-list/list'
            },
            {
                meta: {title: '会员等级'},
                component: 'user/user-level/list'
            },
            {
                meta: {title: '基础设置'},
                component: 'set/base/index'
            },
            {
                meta: {title: '物流设置'},
                component: 'set/express/index'
            },
            {
                meta: {title: '管理员设置'},
                component: 'set/manager/index'
            },
            {
                meta: {title: '交易设置'},
                component: 'set/payment/index'
            }
        ]
    },
    {
        // path: '/login',
        // name: 'login',
        meta: {title: '登陆页'},
        component: 'login/index'
    },
    {
        path: '*',
        redirect: {name:'index'}
    }
]
// 获取路由信息方法
function getRoutes() {
    createRoute(routes)
    return routes
}
// 递归遍历routes数组
function createRoute(arr) {
    for (let i = 0; i < arr.length; i++) {
        if(!arr[i].component) {
            return
        }
        let val = createValue(arr[i].component)
        // 生成name 将/替换为_ 如果有自定义name则按自定义
        arr[i].name = arr[i].name || val.replace(/\//g, "_")
        // 生成path 如果有自定义path则按自定义
        arr[i].path = arr[i].path || `/${val}`
        // 生成component 
        let componentFun = import(`@/views/${arr[i].component}.vue`)
        arr[i].component = () => componentFun
        if(arr[i].children && arr[i].children.length > 0) {
            createRoute(arr[i].children)
        }
    }
}
// 去掉最后的/index字符
function createValue(str) {
    // 获取字符串最后一个/的index
    let index = str.lastIndexOf('/')
    // 获取index字符
    let val = str.substring(index + 1, str.length)
    if (val === 'index') {
        // 截取index前面的字符
        return str.substring(index, -1)
    }
    return str

}
export default getRoutes()